<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '详情'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="新增类型" prop="appType">
      <template>
        <el-radio v-model="dataForm.appType" label="0" @click.native.prevent="change1(0)">安卓客户端</el-radio>
        <el-radio v-model="dataForm.appType" label="1" @click.native.prevent="change2(1)">安卓商家端</el-radio>
      </template>
    </el-form-item>
    <el-form-item label="是否强制升级:" prop="mustBe">
      <template>
        <el-radio v-model="dataForm.mustBe" label="1">强制</el-radio>
        <el-radio v-model="dataForm.mustBe" label="0">不强制</el-radio>
      </template>
    </el-form-item>
    <el-form-item label="主版本号:" prop="newMasterVer">
      <span>{{dataForm.newMasterVer}}</span>
    </el-form-item>
    <el-form-item label="内部版本号:" prop="newAssistVer">
      <span>{{dataForm.newAssistVer}}</span>
    </el-form-item>
    <el-form-item label="下载地址:" prop="downUrl">
      <span>{{dataForm.downUrl}}</span>
    </el-form-item>
    <el-form-item label="上传apk文件:">
      <el-upload
      class="upload-demo"
      :action="this.$http.adornUrl(`/app/version/addApk?token=${this.$cookie.get('token')}&newMasterVer=${this.dataForm.newMasterVer}`)"
      :on-success="handleSuccess"
      :on-exceed="onExceed"
      :beforeUpload="beforeAvatarUpload"
      :limit="1">
      <el-button size="small" type="primary" v-if="this.dataForm.newMasterVer === ''" disabled="disabled">点击上传</el-button>
      <el-button size="small" type="primary" v-if="this.dataForm.newMasterVer != ''" @click="upload()">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">上传前先选择类型，只能上传apk/APK文件，且不超过100MB</div>
     </el-upload>
    </el-form-item>
    <el-form-item label="更新描述:" prop="description">
      <!-- <span>{{dataForm.description}}</span> -->
      <el-input type="textarea" placeholder="请输入内容" v-model="dataForm.description"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          newMasterVer: '',
          newAssistVer: '',
          downUrl: '',
          description: '',
          updateTime: '',
          mustBe: '',
          appType: ''
        },
        dataRule: {
          appType: [
            { required: true, message: '新增类型不能为空', trigger: 'blur' }
          ],
          mustBe: [
            { required: true, message: '是否强制升级不能为空', trigger: 'blur' }
          ],
          newMasterVer: [
            { required: true, message: '主版本号不能为空', trigger: 'blur' }
          ],
          newAssistVer: [
            { required: true, message: '内部版本号不能为空', trigger: 'blur' }
          ],
          downUrl: [
            { required: true, message: '下载地址不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '更新描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 获取安卓用户端新的版本号
      change1 (appType) {
        // if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(`/app/version/getVersionByType/${appType}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.newMasterVer = data.data.newMasterVer
            this.dataForm.newAssistVer = data.data.newAssistVer
            this.dataForm.appType = 0 + ''
          }
        })
        // }
      },
      // 获取安卓商家端新的版本号
      change2 (appType) {
        // if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(`/app/version/getVersionByType/${appType}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.newMasterVer = data.data.newMasterVer
            this.dataForm.newAssistVer = data.data.newAssistVer
            this.dataForm.appType = 1 + ''
          }
        })
        // }
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        // debugger
        this.$message({
          type: 'info',
          message: '文件上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.downUrl = res.data
        }
        // console.log(this.dataForm.imgs)
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个文件',
          duration: 6000
        })
      },
      beforeAvatarUpload (file) {
        var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
        const extension = testmsg === 'apk'
        const extension2 = testmsg === 'APK'
        const isLt = file.size / 1024 / 1024 < 100
        if (!extension && !extension2) {
          this.$message({
            message: '上传文件只能是apk格式!',
            type: 'warning'
          })
        }
        if (!isLt) {
          this.$message({
            message: '上传文件大小不能超过100MB!',
            type: 'warning'
          })
        }
        return extension || extension2
      },
      init () {
        // this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // if (this.dataForm.id) {
          //   this.$http({
          //     url: this.$http.adornUrl(`/app/version/getVersionById/${this.dataForm.id}`),
          //     method: 'get',
          //     params: this.$http.adornParams()
          //   }).then(({data}) => {
          //     if (data && data.code === 0) {
          //       this.dataForm.createTime = data.data.createTime
          //       this.dataForm.masterVer = data.data.masterVer
          //       this.dataForm.assistVer = data.data.assistVer
          //       this.dataForm.downUrl = data.data.downUrl
          //       this.dataForm.description = data.data.description
          //       this.dataForm.updateTime = data.data.updateTime
          //       this.dataForm.mustBe = data.data.mustBe
          //       this.dataForm.appType = data.data.appType
          //     }
          //   })
          // }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/version/${!this.dataForm.id ? 'addAppVersion' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'newMasterVer': this.dataForm.newMasterVer,
                'newAssistVer': this.dataForm.newAssistVer,
                'downUrl': this.dataForm.downUrl,
                'description': this.dataForm.description,
                'mustBe': this.dataForm.mustBe,
                'appType': this.dataForm.appType,
                'strongUpdateApp': 0
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      upload () {
        if (this.dataForm.newMasterVer === '') {
          this.$message({
            showClose: true,
            message: '请先选择类型再进行上传！',
            type: 'warning'
          })
        }
      }
    }
  }
</script>
